package fafu.comedu.workerrepair.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import fafu.comedu.workerrepair.R;
import fafu.comedu.workerrepair.command.TimeChange;
import fafu.comedu.workerrepair.entitys.RecordList;

public class Record_Adapter extends RecyclerView.Adapter<Record_Adapter.ViewHolder> {
    private List<RecordList> recordList;
    private OnItemClickListener mItemClickListener;
    private Context context;

    public Record_Adapter(List<RecordList> recordList, OnItemClickListener mItemClickListener, Context context) {
        this.recordList = recordList;
        this.mItemClickListener = mItemClickListener;
        this.context = context;
    }
    @NonNull
    @Override
    public Record_Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.record_list,parent,false);
        final ViewHolder holder = new ViewHolder(view);

        return holder;

    }

    @Override
    public void onBindViewHolder(@NonNull Record_Adapter.ViewHolder holder, int position) {
        final RecordList recordList = this.recordList.get(position);
        holder.studentName.setText(recordList.getStudentName());
        holder.studentAddr.setText(recordList.getStudentAddr());
        holder.addTime.setText(TimeChange.Long2Date(recordList.getAddTime()));
        holder.msg.setText(recordList.getMsg());
        holder.state.setText(recordList.getState());

        RequestOptions requestOptions = new RequestOptions();
        requestOptions.placeholder(R.mipmap.nopic).error(R.mipmap.nopic);
        if(recordList.getPic()!= null){
//            holder.recordList_pic.setImageDrawable(LoadImageFromWebOperations(recordList.getPic()));
            Glide.with(context).load(recordList.getPic()).apply(requestOptions).into( holder.recordList_pic);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("record", "onClick: "+recordList.getId());
                mItemClickListener.onItemClick(v,recordList.getId());
            }
        });
    }
    Drawable LoadImageFromWebOperations(String url) {
        InputStream is = null;
        Drawable d = null;
        try {
            is = (InputStream) new URL(url).getContent();
            d = Drawable.createFromStream(is, "src name");
            return d;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int getItemCount() {
        return this.recordList.size();
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, Integer id);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView studentName;
        private TextView addTime;
        private TextView msg;
        private TextView studentAddr;
        private TextView state;
        private ImageView recordList_pic;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            studentName = itemView.findViewById(R.id.recordList_studentName);
            studentAddr = itemView.findViewById(R.id.recordList_studentAddr);
            addTime = itemView.findViewById(R.id.recordList_addTime);
            msg = itemView.findViewById(R.id.recordList_msg);
            state = itemView.findViewById(R.id.recordList_state);
            recordList_pic = itemView.findViewById(R.id.recordList_pic);
        }
    }
}
